<?php
	//include vars and db
	require("../setup.inc.php");
	
	if(isset($_REQUEST['points'])) {
		header("Content-type: text/xml");
		print "<?xml version = \"1.0\"?>\n\t<response>\n";
		$driver = $_REQUEST['driver'];
		$points = $_REQUEST['numpoints'];
		$reason = $_REQUEST['reason'];

		//insert new points
		$sql = sprintf("INSERT INTO `t_seniority` ( `driver_id` , `points` , `entry_by` , `description` ) VALUES ( %s, %s, %s, %s )",quote_smart($driver),quote_smart($points),quote_smart($_SESSION['driver_id']),quote_smart($reason));
		$result = mysql_query($sql);
		if(mysql_affected_rows() != 1) {
			//we had a problem adding it into the db.. let 'em know
			print "\t\t<error>1</error>\n";
			print "\t\t<msg>Error adding points to database.</msg>\n";
			print "\t\t<debug>MySQL said: " . mysql_errno() . " - " . mysql_error() . " (" . $sql . ")</debug>\n";
			print "\t</response>\n";
			exit;
		}
		
		//determine current points
		$sql = "SELECT sum( points ) AS total_points FROM `t_seniority` WHERE driver_id = " . quote_smart($driver);
		$result = mysql_query($sql);
		if(mysql_num_rows($result) == 1) {
			$data = mysql_fetch_assoc($result);
			print "\t\t<error>0</error>\n";
			print "\t\t<msg>Points were adjusted in database.</msg>\n";
			print "\t\t<total>" . $data['total_points'] . "</total>\n";
			print "\t</response>\n";
			exit;
		} else {
			print "\t\t<error>1</error>\n";
			print "\t\t<msg>There was an error determining the user's seniority point balance. However, the points were added.</msg>\n";
			print "\t\t<debug>MySQL said: " . mysql_errno() . " - " . mysql_error() . " (" . $sql . ")</debug>\n";
			print "\t</response>\n";
			exit;
		}		
		exit;
	}
	
	//if this is the updateFields script, run these ifs
	if(isset($_REQUEST['updateField'])) {
		header("Content-type: text/xml");
		print "<?xml version = \"1.0\"?>\n\t<response>\n";
		$value = $_REQUEST['value'];
		print "\t\t<value>" . $value . "</value>\n";
		switch ($_REQUEST['updateField']) {
			case '1b2e4a850f7951428f949bd381c2f25e':  //email
				//validate
				if(!check_email_address($value)) {
					print "\t\t<error>1</error>\n";
					print "\t\t<msg>You have entered an invalid email address.</msg>\n";
					print "\t</response>\n";
					exit;
				}
				//format - not needed
				$formattedValue = $value;
				print "\t\t<result>" . $formattedValue . "</result>\n";
				//check for change
				if($formattedValue == driverDetail($_SESSION['driver_id'],'email')) {
					//no update is necessary. They are trying to change it to the same thing...
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>No update necessary.</msg>\n";
					print "\t</response>\n";
					exit;
				}
				//update
				$updateResult = updateDetail('t_drivers','email','`driver_id` = ' . $_SESSION['driver_id'],$formattedValue);
				if($updateResult === true) {
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>Update successful.</msg>\n";
					print "\t</response>\n";
					exit;
				} else {
					print "\t\t<error>1</error>\n";
					print "\t\t<msg>An error occurred while saving your change.</msg>\n";
					print "\t\t<debug>" . $updateResult . "</debug>\n";
					print "\t</response>\n";
					exit;
				}
			break;
			case '7ed32b6037a460fc97a81c9e569d815b':  //screenname
				//validate - nope
				//format - not needed
				$formattedValue = $value;
				print "\t\t<result>" . $formattedValue . "</result>\n";
				//check for change
				if($formattedValue == driverDetail($_SESSION['driver_id'],'screenname')) {
					//no update is necessary. They are trying to change it to the same thing...
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>No update necessary.</msg>\n";
					print "\t</response>\n";
					exit;
				}
				//update
				$updateResult = updateDetail('t_drivers','screenname','`driver_id` = ' . $_SESSION['driver_id'],$formattedValue);
				if($updateResult === true) {
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>Update successful.</msg>\n";
					print "\t</response>\n";
					exit;
				} else {
					print "\t\t<error>1</error>\n";
					print "\t\t<msg>An error occurred while saving your change.</msg>\n";
					print "\t\t<debug>" . $updateResult . "</debug>\n";
					print "\t</response>\n";
					exit;
				}
			break;
			case 'a3911fb0c6cb6cc857e2aed25f17e168':  //cellphone
				//validate
				if(strlen(ereg_replace ('[^0-9]+', '', $value)) != 10) {
					print "\t\t<error>1</error>\n";
					print "\t\t<msg>You must enter a 10 digit phone number.</msg>\n";
					print "\t</response>\n";
					exit;
				}
				//format
				$formattedValue = ereg_replace ('[^0-9]+', '', $value);
				print "\t\t<result>" . formatPhone($formattedValue). "</result>\n";
				//check for change
				if($formattedValue == driverDetail($_SESSION['driver_id'],'cellphone')) {
					//no update is necessary. They are trying to change it to the same thing...
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>No update necessary.</msg>\n";
					print "\t</response>\n";
					exit;
				}
				//update
				$updateResult = updateDetail('t_drivers','cellphone','`driver_id` = ' . $_SESSION['driver_id'],$formattedValue);
				if($updateResult === true) {
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>Update successful.</msg>\n";
					print "\t</response>\n";
					exit;
				} else {
					print "\t\t<error>1</error>\n";
					print "\t\t<msg>An error occurred while trying to save your changes.</msg>\n";
					print "\t\t<debug>" . $updateResult . "</debug>\n";
					print "\t</response>\n";
					exit;
				}
			break;
			case 'e6ba6a95e92ab02920655918863e5bf8': //campus phone?
				//validate
				if(strlen(ereg_replace ('[^0-9]+', '', $value)) != 10) {
					print "\t\t<error>1</error>\n";
					print "\t\t<msg>You must enter a 10 digit phone number.</msg>\n";
					print "\t</response>\n";
					exit;
				}
				//format
				$formattedValue = ereg_replace ('[^0-9]+', '', $value);
				print "\t\t<result>" . formatPhone($formattedValue). "</result>\n";
				//check for change
				if($formattedValue == driverDetail($_SESSION['driver_id'],'campusphone')) {
					//no update is necessary. They are trying to change it to the same thing...
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>No update necessary.</msg>\n";
					print "\t</response>\n";
					exit;
				}
				//update
				$updateResult = updateDetail('t_drivers','campusphone','`driver_id` = ' . $_SESSION['driver_id'],$formattedValue);
				if($updateResult === true) {
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>Update successful.</msg>\n";
					print "\t</response>\n";
					exit;
				} else {
					print "\t\t<error>1</error>\n";
					print "\t\t<msg>An error occurred while trying to save your changes.</msg>\n";
					print "\t\t<debug>" . $updateResult . "</debug>\n";
					print "\t</response>\n";
					exit;
				}
			break; 
			case '8479166e9d8fd7abf3e41a1bfebfb0b7': //home phone?
				//validate
				if(strlen(ereg_replace ('[^0-9]+', '', $value)) != 10) {
					print "\t\t<error>1</error>\n";
					print "\t\t<msg>You must enter a 10 digit phone number.</msg>\n";
					print "\t</response>\n";
					exit;
				}
				//format
				$formattedValue = ereg_replace ('[^0-9]+', '', $value);
				print "\t\t<result>" . formatPhone($formattedValue). "</result>\n";
				//check for change
				if($formattedValue == driverDetail($_SESSION['driver_id'],'homephone')) {
					//no update is necessary. They are trying to change it to the same thing...
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>No update necessary.</msg>\n";
					print "\t</response>\n";
					exit;
				}
				//update
				$updateResult = updateDetail('t_drivers','homephone','`driver_id` = ' . $_SESSION['driver_id'],$formattedValue);
				if($updateResult === true) {
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>Update successful.</msg>\n";
					print "\t</response>\n";
					exit;
				} else {
					print "\t\t<error>1</error>\n";
					print "\t\t<msg>An error occurred while trying to save your changes.</msg>\n";
					print "\t\t<debug>" . $updateResult . "</debug>\n";
					print "\t</response>\n";
					exit;
				}
			break;
			case 'adff44c5102fca279fce7559abf66fee': //physical exp
				//validate - not really
				//format
				$formattedValue = date("Ymd",strtotime($value));
				print "\t\t<result>" . date("M j, Y",strtotime($value)). "</result>\n";
				//check for change
				if($formattedValue == driverDetail($_SESSION['driver_id'],'physicalexp')) {
					//no update is necessary. They are trying to change it to the same thing...
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>No update necessary.</msg>\n";
					print "\t</response>\n";
					exit;
				}
				//update
				$updateResult = updateDetail('t_drivers','physicalexp','`driver_id` = ' . $_SESSION['driver_id'],$formattedValue);
				if($updateResult === true) {
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>Update successful.</msg>\n";
					print "\t</response>\n";
					exit;
				} else {
					print "\t\t<error>1</error>\n";
					print "\t\t<msg>An error occurred while trying to save your changes.</msg>\n";
					print "\t\t<debug>" . $updateResult . "</debug>\n";
					print "\t</response>\n";
					exit;
				}
			break;
			case '6209804952225ab3d14348307b5a4a27': //licenseexp 
				//validate - not really
				//format
				$formattedValue = date("Ymd",strtotime($value));
				print "\t\t<result>" . date("M j, Y",strtotime($value)). "</result>\n";
				//check for change
				if($formattedValue == driverDetail($_SESSION['driver_id'],'licenseexp')) {
					//no update is necessary. They are trying to change it to the same thing...
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>No update necessary.</msg>\n";
					print "\t</response>\n";
					exit;
				}
				//update
				$updateResult = updateDetail('t_drivers','licenseexp','`driver_id` = ' . $_SESSION['driver_id'],$formattedValue);
				if($updateResult === true) {
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>Update successful.</msg>\n";
					print "\t</response>\n";
					exit;
				} else {
					print "\t\t<error>1</error>\n";
					print "\t\t<msg>An error occurred while trying to save your changes.</msg>\n";
					print "\t\t<debug>" . $updateResult . "</debug>\n";
					print "\t</response>\n";
					exit;
				}
			break;
			case '8d08068cbfbd61eae31e0c6aa4bdceec': //graduation date
				//validate - not really
				//format
				$formattedValue = date("Ym",strtotime($value)) . '01';
				print "\t\t<result>" . date("M j, Y",strtotime($formattedValue)). "</result>\n";
				//check for change
				if($formattedValue == driverDetail($_SESSION['driver_id'],'graduationdate')) {
					//no update is necessary. They are trying to change it to the same thing...
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>No update necessary.</msg>\n";
					print "\t</response>\n";
					exit;
				}
				//update
				$updateResult = updateDetail('t_drivers','graduationdate','`driver_id` = ' . $_SESSION['driver_id'],$formattedValue);
				if($updateResult === true) {
					print "\t\t<error>0</error>\n";
					print "\t\t<msg>Update successful.</msg>\n";
					print "\t</response>\n";
					exit;
				} else {
					print "\t\t<error>1</error>\n";
					print "\t\t<msg>An error occurred while trying to save your changes.</msg>\n";
					print "\t\t<debug>" . $updateResult . "</debug>\n";
					print "\t</response>\n";
					exit;
				}
			break;
		}
		exit;
	}


	//if this is the schedule program, run this if..
	if($_REQUEST['task'] == 'sched_maker_disp_days') {
		outputDayList($_REQUEST['group_id']);
		//only this if!
		exit;
	}

	if($_REQUEST['task'] == 'sched_maker_disp_lines') {
		outputLineList($_REQUEST['table_id']);
		exit;
	}
	
	if($_REQUEST['task'] == 'moveItemUp') {
		$table = $_REQUEST['table'];
		$column = $_REQUEST['column'];
		$where = $_REQUEST['where'];
		$clause = $_REQUEST['clause'];
		$primary = $_REQUEST['primary'];
		$subject = $_REQUEST['subject'];
		reorderTable($table,$where,$clause,$column,$primary);
		$result = mysql_query(sprintf("UPDATE `$table` SET `$column` = `$column` - 1.5 WHERE `$primary` = %s",quote_smart($subject)));
		reorderTable($table,$where,$clause,$column,$primary);
		if($table == 't_sched_main')
			outputDayList($clause);
		if($table == 't_sched_structure')
			outputLineList($clause);
		exit;
	}
	if($_REQUEST['task'] == 'moveItemDown') {
		$table = $_REQUEST['table'];
		$column = $_REQUEST['column'];
		$where = $_REQUEST['where'];
		$clause = $_REQUEST['clause'];
		$primary = $_REQUEST['primary'];
		$subject = $_REQUEST['subject'];
		reorderTable($table,$where,$clause,$column,$primary);
		$result = mysql_query(sprintf("UPDATE `$table` SET `$column` = `$column` + 1.5 WHERE `$primary` = %s",quote_smart($subject)));
		reorderTable($table,$where,$clause,$column,$primary);
		if($table == 't_sched_main')
			outputDayList($clause);
		if($table == 't_sched_structure')
			outputLineList($clause);
		exit;
	}
	
	if(isset($_REQUEST['grabShiftDates'])) {
		//this grabs shift dates for the workswap board.
		$sql = "SELECT date_id
				FROM `t_sched_dates`, `t_sched_structure`, `t_sched_shifts`
				WHERE `t_sched_dates`.`table_id` = `t_sched_structure`.`table_id`
				  AND `t_sched_structure`.`structure_id` = `t_sched_shifts`.`structure_id`
				  AND `t_sched_shifts`.`shift_id` = " . quote_smart($_REQUEST['grabShiftDates']) . "
				  AND `t_sched_dates`.`date_id` > " . date("Ymd") . "
				ORDER BY `t_sched_dates`.`date_id`";
		$result = mysql_query($sql);
		if(mysql_num_rows($result) > 0) {
			print "<select name='tempDate'>\n";
			while($row = mysql_fetch_assoc($result)) {
				print "\t<option value='" . $row['date_id'] . "'>" . formatDate($row['date_id']) . "</option>\n";
			}
			print "</select>\n";
		} else {
			print "There are no dates for this shift.";
		}
		exit;
	}
	
	//grab start and end times
	$sql = sprintf("SELECT *
					FROM `t_sched_shifts`
					WHERE `shift_id` = %s",
						quote_smart($_REQUEST['shiftId']));
	$shift_data = mysql_query($sql);
	if(mysql_num_rows($shift_data) != 1) { die("Invalid Shift"); }
	$shift_info = mysql_fetch_assoc($shift_data);
	
	//grab all time points
	$sql = sprintf("SELECT `t_sched_dc_times`.`dc_time`
			FROM `t_sched_shifts` , `t_sched_structure` , `t_sched_dc_times`
			WHERE `t_sched_shifts`.`structure_id` = `t_sched_structure`.`structure_id`
			AND `t_sched_structure`.`dc_group_id` = `t_sched_dc_times`.`dc_group_id`
			AND `t_sched_shifts`.`shift_id` = %s 
			AND `t_sched_dc_times`.`dc_time` > %s
			AND `t_sched_dc_times`.`dc_time` < %s
			ORDER BY `t_sched_dc_times`.`dc_time` ASC",
				quote_smart($_REQUEST['shiftId']),
				quote_smart($shift_info['shift_start']),
				quote_smart($shift_info['shift_end']));
	$timepoints = mysql_query($sql);
	
	if(DEBUG) print "Returned " . mysql_num_rows($timepoints) . " rows.";
	
	//create output
	$times = array();
	//add start time
	$options = "<option value='" . $shift_info['shift_start'] . "'>" . $shift_info['shift_start'] . "</option>\n";
	while($row = mysql_fetch_assoc($timepoints)) {
		$options .= "<option value='" . $row['dc_time'] . "'>" . $row['dc_time'] . "<br>\n";
	}
	$options .= "<option value='" . $shift_info['shift_end'] . "'>" . $shift_info['shift_end'] . "</option>\n";
	
	

?>
Workswap Start <select name=start><?= $options ?></select> Workswap End <select name=end><?= $options ?></select>



<?php

	function reorderTable($table,$where,$clause,$column,$primary) {
		$result = mysql_query(sprintf("SELECT * FROM `$table` WHERE `$where` = %s ORDER BY `$column`",quote_smart($clause)));
		$count = 0;
		while($row = mysql_fetch_assoc($result)) {
			$inner_result = mysql_query(sprintf("UPDATE `$table` SET `$column` = %s WHERE `$primary` = %s",quote_smart($count),quote_smart($row[$primary])));
			$count ++;
		}
	}
	
	function outputDayList ($group) {
		?>
		<table style="margin: auto; border: 1px dashed black; ">
			<tr>
				<th>Day Title</th>
				<th>Order</th>
			</tr>
		<?php
						$sql = sprintf("SELECT * FROM `t_sched_main` WHERE `group_id` = %s ORDER BY `order`",quote_smart($group));
						if(DEBUG) print $sql;
						$result = mysql_query($sql);
						while($row = mysql_fetch_assoc($result)) {
					?>			
			<tr>
				<td style="text-align:left; "><a href="/index.php?sched_maker=1&amp;day=<?= $row['table_id'] ?>&amp;level=day"><?= $row['description'] ?></a></td>
				<td><img src='<?= ICON_DOWN ?>' onClick="moveDown('t_sched_main','group_id',<?=$group?>,'order','table_id',<?= $row['table_id'] ?>)" height=16 width=16 border=0 alt='move_down'> <img src='<?= ICON_UP ?>' onClick="moveUp('t_sched_main','group_id',<?=$group?>,'order','table_id',<?= $row['table_id'] ?>)" height=16 width=16 border=0 alt='move_up'></td>
			</tr>
					<?php
						}
						mysql_free_result($result);
		
		
		
		?>
			<tr>
				<td colspan="2">Create New Day</td>
			</tr>
		</table>

		<?php
		
	}

	function outputLineList ($day) {
		?>
		<table style="margin: auto; border: 1px dashed black; ">
			<tr>
				<th>Line Title</th>
				<th>Line Time</th>
				<th>Order</th>
			</tr>
		<?php
						$sql = sprintf("SELECT `structure_id`,`description`,`start`,`end` FROM `t_sched_structure` WHERE `table_id` = %s ORDER BY `order`",quote_smart($day));
						if(DEBUG) print $sql;
						$result = mysql_query($sql);
						while($row = mysql_fetch_assoc($result)) {
					?>			
			<tr>
				<td style="text-align:left; "><a href="/index.php?sched_maker=1&amp;line=<?= $row['structure_id'] ?>&amp;level=line"><?= $row['description'] ?></a></td>
				<td style="text-align:left; "><?= $row['start'] ?> - <?= $row['end'] ?></td>
				<td><img src='<?= ICON_DOWN ?>' onClick="moveDown('t_sched_structure','table_id',<?=$day?>,'order','structure_id',<?= $row['structure_id'] ?>)" height=16 width=16 border=0 alt='move_down'> <img src='<?= ICON_UP ?>' onClick="moveUp('t_sched_structure','table_id',<?=$day?>,'order','structure_id',<?= $row['structure_id'] ?>)" height=16 width=16 border=0 alt='move_up'></td>
			</tr>
					<?php
						}
						mysql_free_result($result);
		
		
		
		?>
			<tr>
				<td colspan="3">Create New Line</td>
			</tr>
		</table>

		<?php
		
	}


?>
